package xyz.k1998.kbasic.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import xyz.k1998.kbasic.bean.Grade;
import xyz.k1998.kbasic.dao.GradeDao;

import java.util.HashMap;
import java.util.List;

@RestController
public class GradeController {
    @Autowired
    GradeDao gradeDao;


    @PostMapping("/allGrades")
    public String allGrades() {
        List<Grade> grades = gradeDao.getAllGrade();
        HashMap<String, Object> res = new HashMap<>();
        res.put("data",grades);
        String mysql_res = JSON.toJSONString(res);
        return mysql_res;
    }

    @PostMapping("/addGrade")
    public String addGrade(@RequestBody Grade grade) {
        int i = gradeDao.addGrade(grade);
        String str = i >0?"success":"error";
        return str;
    }

    @PostMapping("/updateGrade")
    public String updateGrade(@RequestBody Grade grade){
        int i = gradeDao.updateGrade(grade);
        String str = i >0?"success":"error";
        return str;
    }

    @PostMapping("/deleteGrade")
    public String deleteGrade(Integer id){
        int i = gradeDao.deleteGrade(id);
        String str = i >0?"success":"error";
        return str;
    }


}
